package org.autumn.design.state;

public class Context {
    private State state;

    public Context() {
        state = new ClosedState();
    }

    public void setState(State state) {
        this.state = state;
    }

    public State getState() {
        return state;
    }

    public void open(){
        state.opened(this);
    }

    public void close(){
        state.closed(this);
    }

    public static void main(String[] args) {
        Context context = new Context();

        context.close();
        context.open();

    }
}

interface State {
    void opened(Context context);
    void closed(Context context);
}


class OpenedState implements State {

    @Override
    public void opened(Context context) {
        System.out.println("the door is open");
    }


    @Override
    public void closed(Context context) {
        System.out.println("door closed ...");
        context.setState(new ClosedState());
    }

}


class ClosedState implements State {

    @Override
    public void opened(Context context) {
        System.out.println("door opened ...");
        context.setState(new OpenedState());
    }


    @Override
    public void closed(Context context) {
        System.out.println("the door is close");
    }

}